package com.atguigu.gmall.web.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.item.client.ItemClientFeign;
import com.atguigu.gmall.model.product.BaseCategoryView;
import com.atguigu.gmall.product.client.ProductClientController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author: lx
 * @Time: 16:11
 * @Description:
 */
@Controller
public class ItemController {

    @Autowired
    private ItemClientFeign itemClientFeign;
    @Autowired
    private ProductClientController productClientController;

    @Autowired
    TemplateEngine templateEngine;

    @GetMapping("/{skuId}.html")
    public String itemPage(@PathVariable(name = "skuId") Long skuId, Model model){
        Map<String, Object> resultMap = itemClientFeign.getItem(skuId);
        model.addAllAttributes(resultMap);
        //model.add
        return "item/index";
    }

    @GetMapping("/index")
    public String shouye(Model model){
        List<Map<String, Object>> data = this.getData();
        model.addAttribute("list", data);

        return "index/index.html";
    }


    @GetMapping("/")
    public String index() {
        return "index";
    }

    @GetMapping("/createHtml")
    @ResponseBody
    public Result createHtml() {
        //数据
        Context iContext = new Context();
        List<Map<String, Object>> listMap = getData();
        iContext.setVariable("list", listMap);
        //流 写入流  不要乱
        Writer writer = null;
        try {
            //writer = new FileWriter("D:\\temp\\index.html");
//            writer = new OutputStreamWriter(new
//                    FileOutputStream("D:\\temp\\index.html"),
//                    "UTF-8");
            writer = new PrintWriter(new File("D:\\temp\\index.html"),"UTF-8");
            //生成首页
            //参数1： 页面"index/index"
            //参数2：数据
            //参数3：流  写操作
            templateEngine.process("index/index.html"
                    , iContext, writer);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != writer)
                    writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return Result.ok();
    }

    private List<Map<String, Object>> getData() {
        List<BaseCategoryView> baseCategoryViews = productClientController.getIndex();

        List<Map<String, Object>> maps1 = new ArrayList<>();
        Map<Long, List<BaseCategoryView>> collect = baseCategoryViews.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));
        int index = 0;

        for (Map.Entry<Long, List<BaseCategoryView>> longListEntry : collect.entrySet()) {
            Map<String ,Object> hashMap1 = new HashMap<>();
            List<BaseCategoryView> value1 = longListEntry.getValue();
            //一级商品属性
            hashMap1.put("index",++index);
            hashMap1.put("categoryId",longListEntry.getKey());
            hashMap1.put("categoryName",value1.get(0).getCategory1Name());
            Map<Long, List<BaseCategoryView>> collect1 = value1.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
            List<Map<String, Object>> maps2 = new ArrayList<>();
            for (Map.Entry<Long, List<BaseCategoryView>> listEntry : collect1.entrySet()) {
                Map<String ,Object> hashMap2 = new HashMap<>();
                //获取二级商品属性
                List<BaseCategoryView> value2 = listEntry.getValue();
                hashMap2.put("categoryId",longListEntry.getKey());
                hashMap2.put("categoryName",value2.get(0).getCategory2Name());
                List<Map<String, Object>> maps3 = new ArrayList<>();
                for (BaseCategoryView baseCategoryView : value2) {
                    Map<String ,Object> hashMap3 = new HashMap<>();
                    hashMap3.put("categoryId",baseCategoryView.getCategory3Id());
                    hashMap3.put("categoryName",baseCategoryView.getCategory3Name());
                    maps3.add(hashMap3);
                }
                hashMap2.put("categoryChild",maps3);
                maps2.add(hashMap2);
            }
            hashMap1.put("categoryChild",maps2);
            maps1.add(hashMap1);
        }
        return maps1;
    }

}
